package com.chen.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.common.Result;
import com.chen.entity.OperationLog;
import com.chen.mapper.OperationLogMapper;
import com.chen.service.impl.OperationLogServiceImpl;
import com.mongodb.client.result.DeleteResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 日志控制器
 *
 * @author yezhiqiu
 * @date 2021/07/27
 */
@Api(value = "LogController", tags = "日志模块")
@RestController
@RequestMapping("/log")
public class LogController {
    @Resource
    private OperationLogServiceImpl operationLogService;

    @Autowired
    private MongoTemplate mongoTemplate;


    @Autowired
    private OperationLogMapper operationLogMapper;

    private static final Logger log= LoggerFactory.getLogger(LogController.class);

    @GetMapping("/findPage")
    @ApiOperation(value = "分页")
    public Result findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "5") Integer pageSize,
                           @RequestParam(defaultValue = "") String optModule,
                           @RequestParam(defaultValue = "") String optMethod) {

        return Result.success(operationLogService.findPage(pageNum, pageSize, optModule, optMethod));
    }


    @DeleteMapping("/deleteById/{id}")
    @ApiOperation(value = "删除")
    public Result deleteById(@PathVariable("id") String id) {

        Query query = new Query(Criteria.where("_id").is(id));
        operationLogMapper.deleteById(id);
        return Result.success();
    }


    @PostMapping(value = "/del/batch")
    @ApiOperation("批量删除")
    public Result deleteBatch(@RequestBody List<String> ids) {
//        List<String> collect = Arrays.asList(ids);
        for (String s : ids) {
            Query query = new Query(Criteria.where("_id").is(s));
            operationLogMapper.deleteById(s);
        }
      /*  Query query = new Query();
        //设置条件
        Criteria criteria = new Criteria();
        criteria.and("_id").in(ids);
        query.addCriteria(criteria);*/
        //调用方法,DeleteResult中可以获取到删除的条数
        return Result.success();
    }







}
